package db

import (
	db "hulk-api/databases"
	"log"
)
type Db struct {
	Name string		`json:"name"`
	Instance string		`json:"instance"`
	Jobnum int		`json:"jobnum"`
	Ip string			`json:"ip"`

}


func  GetDbList() ( lists []Db, err error) {
	lists = make([]Db,0)
	rows, err := db.SqlDB.Query("select name,instance,ip from dbs")
	if err != nil {
		log.Println(err)
	}
	defer rows.Close()

	for rows.Next() {
		var d Db
		rows.Scan(&d.Name,&d.Instance,&d.Ip)
		var j Job
		j.Dbname = d.Name
		d.Jobnum = len(j.GetBrotherJobs().UndealJobs().Undeal)
		lists = append(lists, d)
	}
	if err = rows.Err(); err != nil {
		log.Println(err)
		return
	}
	return
}

func (d Db) GetDbListByInstance() ( lists []Db, err error) {
	lists = make([]Db,0)
	//sql := fmt.Sprintf(" select account,name,email,department,role,lastLoginTime from user")
	rows, err := db.SqlDB.Query("select name from dbs where instance = ?" , d.Instance)
	if err != nil {
		log.Println(err)
		return
	}
	defer rows.Close()

	for rows.Next() {
		var dlist Db
		rows.Scan(&dlist.Name)
		lists = append(lists, dlist)
	}
	if err = rows.Err(); err != nil {
		return
	}
	return
}